package cn.josewu.config.CustomerPartitioner;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * @program: covid19
 * @description:  自定义分区器指定分区规则
 * @author: Mr. Tigger
 * @create 2020-11-12 11:04
 **/
public class CustomerPartitioner implements Partitioner {


    @Override
    public int partition(String topic, Object key, byte[] bytes, Object value, byte[] valueBytes, Cluster cluster) {

        Integer k = (Integer) key;
        Integer num = cluster.partitionCountForTopic(topic);
        int partition = k % num;
        return partition;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
